#include "applet.h"

#include <qdebug.h>
#include "ui_mainwindow.h"
#include "util/tool.h"
#include <QStyleFactory>
#define BUTTON_TYPE "btnType"
#define BUTTON_TYPE_NAVIGATION "navigation"
#define BUTTON_TYPE_CUR_NAVIGATION "cur_navigation"


Applet::Applet(QWidget* parent)
    : QMainWindow(parent)
      , ui(new Ui::MainWindow)
{
    empty_qss_ = QStyleFactory::create("");
    ui->setupUi(this);
    setWindowTitle(tr("NetFileTransfer"));
    setStyleSheet(NetFileTransfer::readTextFile(":/qss/light/qt.css"));
    setWindowIcon(QIcon(":/icon/logo.svg"));
    initNavigator();
}

Applet::~Applet()
{
    delete ui;
}

void Applet::setToNavitorBtn(QToolButton* btn, const QString& icon_file, const char* text, int index)
{
    btn->setIcon(QIcon(icon_file));
    btn->setText(tr(text));
    btn->setProperty(BUTTON_TYPE, BUTTON_TYPE_NAVIGATION);
    connect(btn, &QToolButton::clicked, [this,btn, index](bool checked)
    {
        changeMainView(btn, index);
    });
}

void Applet::changeMainView(QToolButton* naviBtn, int index)
{
    std::lock_guard l(current_navi_lock_);
    if (current_navi_)
    {
        current_navi_->setProperty(BUTTON_TYPE, BUTTON_TYPE_NAVIGATION);
        current_navi_->setStyle(empty_qss_);
    }
    naviBtn->setProperty(BUTTON_TYPE, BUTTON_TYPE_CUR_NAVIGATION);
    current_navi_ = naviBtn;
    current_navi_->setStyle(empty_qss_);
    ui->stacked_widget_->setCurrentIndex(index);
}

void Applet::initNavigator()
{
    home_view_ = new HomeView(this);
    setting_view_ = new SettingView(this);
    task_view_ = new TaskView(this);
    setToNavitorBtn(ui->home_navi_btn_, ":/icon/folder.svg", "文件", 0);
    ui->stacked_widget_->addWidget(home_view_);
    setToNavitorBtn(ui->task_navi_btn_, ":/icon/down-up.svg", "传输", 1);
    ui->stacked_widget_->addWidget(task_view_);
    setToNavitorBtn(ui->setting_navi_btn_, ":/icon/setting.svg", "设置", 2);
    ui->stacked_widget_->addWidget(setting_view_);
    changeMainView(ui->home_navi_btn_, 0);
}